package com.kmproject.haifu.mvp.ui.widget.pullRefresh;

import android.content.Context;
import android.support.annotation.NonNull;
import android.util.AttributeSet;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

/**
 * Created by KevinLee on 2019-08-15.
 */
public class PullToRefreshView extends SmartRefreshLayout {
    private int mCurPageNo = 1, mMaxPageNo = 1;
    private int mCurCount = 0, mPageSize = 12;
    private boolean isClearData = true;
    private PullToRefreshView pullToRefreshView;

    public PullToRefreshView(Context context) {
        super(context);
        pullToRefreshView = this;
    }

    public PullToRefreshView(Context context, AttributeSet attrs) {
        super(context, attrs);
        pullToRefreshView = this;
    }

    public PullToRefreshView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        pullToRefreshView = this;
    }

    public void setmCurCount(int count) {
        mCurCount = count;
    }

    public void setmPageSize(int totalCount) {
        mPageSize = totalCount;
    }

    public void setCurPageNo(int no) {
        mCurPageNo = no;
    }

    public void setMaxPageNo(int totalPgeNo) {
        mMaxPageNo = totalPgeNo;
    }

    public boolean isClearData() {
        return isClearData;
    }

    private OnRefreshListener2 refreshListener2;
    private OnLoadMoreListener2 loadMoreListener2;
    private OnRefreshLoadMoreListener2 refreshLoadMoreListener2;

    public RefreshLayout setOnRefreshListener2(OnRefreshListener2 listener) {
        refreshListener2 = listener;
        setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mCurPageNo = 1;
                isClearData = true;
                refreshListener2.onRefresh(refreshLayout, mCurPageNo);
            }
        });
        return this;
    }

    public RefreshLayout setOnLoadMoreListener2(OnLoadMoreListener2 listener) {
        loadMoreListener2 = listener;
        mEnableLoadMore = true;
        setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                isClearData = false;
                if (mCurCount != 0) {
                    if (mCurCount == mPageSize) {
                        mCurPageNo++;
                        loadMoreListener2.onLoadMore(refreshLayout, mCurPageNo);
                    } else {
                        pullToRefreshView.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                finishLoadMoreWithNoMoreData();
                            }
                        }, 50);
                    }
                } else {
                    if (mCurPageNo < mMaxPageNo) {
                        mCurPageNo++;
                        loadMoreListener2.onLoadMore(refreshLayout, mCurPageNo);
                    } else {
                        pullToRefreshView.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                finishLoadMoreWithNoMoreData();
                            }
                        }, 50);
                    }
                }
            }
        });
        return this;
    }

    public RefreshLayout setOnRefreshLoadMoreListener2(OnRefreshLoadMoreListener2 listener) {
        refreshLoadMoreListener2 = listener;
        setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mCurPageNo = 1;
                isClearData = true;
                refreshLoadMoreListener2.onRefresh(refreshLayout, mCurPageNo);
            }
        });
        mEnableLoadMore = true;
        setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                isClearData = false;
                if (mCurCount != 0) {
                    if (mCurCount == mPageSize) {
                        mCurPageNo++;
                        refreshLoadMoreListener2.onLoadMore(refreshLayout, mCurPageNo);
                    } else {
                        pullToRefreshView.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                finishLoadMoreWithNoMoreData();
                            }
                        }, 50);
                    }
                } else {
                    if (mCurPageNo < mMaxPageNo) {
                        mCurPageNo++;
                        refreshLoadMoreListener2.onLoadMore(refreshLayout, mCurPageNo);
                    } else {
                        pullToRefreshView.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                finishLoadMoreWithNoMoreData();
                            }
                        }, 50);
                    }
                }
            }
        });
        return this;
    }
}
